#!/usr/bin/python3 
# -*- coding:utf-8 -*-

import random


def binary_search(ls, val):
    low = 0 
    high = len(ls) - 1 
    count = 0 

    while low <= high: 
        count += 1 
        mid = (low + high) // 2 
    
        if val == ls[mid]: 
            return f"你要查找的{val}的索引值是{mid}, 共找了{count}次。" 
        
        if val < ls[mid]: 
            high = mid - 1 
        else: 
            low = mid + 1 
    
    else: 
        return f"你要查找的{val}不存在！" 


lst = sorted([random.randint(-100, 100) for _ in range(10)]) 
print('查询的数据列表>>>', lst) 
value = random.choice(lst) 
res = binary_search(lst, value) 
print(res)